package com.xiaolun.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author 硕鼠
 * @Date 2020/6/13 18:58
 * @Version 1.0
 * @Description 文件说明
 */
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        //强转，以便HttpServletRequest/HttpServletResponse类型的对象正常使用session。
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        //对没有session对象的用户进行拦截
        if (request.getSession().getAttribute("USER_SESSION") == null){
            response.sendRedirect("/s1/error.jsp");
        }else {
            chain.doFilter(req, resp);
        }
    }

    @Override
    public void destroy() {

    }
}
